package com.wisecoach.gatewayplus.spring.boot.autoconfigure;

import com.wisecoach.gatewayplus.event.FilteredBlockSource;
import com.wisecoach.gatewayplus.spring.boot.autoconfigure.event.GatewayPlusEventSourceConfiguration;
import com.wisecoach.gatewayplus.spring.boot.autoconfigure.event.GatewayPlusMQEventConfiguration;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;

import java.util.function.Predicate;

/**
 * {@code @author:} wisecoach
 * {@code @date:} 2023/9/22 下午12:07
 * {@code @version:} 1.0.0
 */
public class GatewayPlusEventImportSelector implements ImportSelector {

    private static final boolean USE_EVENT_SOURCE = ClassUtils.isPresent(FilteredBlockSource.class.getName(),
            GatewayPlusEventImportSelector.class.getClassLoader());
    private static final boolean MQ_AS_EVENT_SOURCE = ClassUtils.isPresent(StreamBridge.class.getName(),
            GatewayPlusEventImportSelector.class.getClassLoader());

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        if (USE_EVENT_SOURCE) {
            return new String[]{GatewayPlusEventSourceConfiguration.class.getName()};
        } else if (MQ_AS_EVENT_SOURCE) {
            return new String[]{GatewayPlusMQEventConfiguration.class.getName()};
        } else {
            return new String[]{};
        }
    }

    @Override
    public Predicate<String> getExclusionFilter() {
        return null;
    }
}
